package com.futao.techsharingmq.basic.concept.simple.book;

import com.futao.techsharingmq.basic.concept.simple.MqChannelUtils;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeoutException;

/**
 * exclusive队列，在channel和connection断开之后会自动删除队列，队列里面的消息也会丢失
 *
 * @author futao@gmail.com
 * @date 2021/11/2
 */
public class EQ {
    public static void main(String[] args) throws IOException, InterruptedException, TimeoutException {
        Channel channel = MqChannelUtils.createChannel();
        AMQP.Exchange.DeclareOk eq_exchange = channel.exchangeDeclare("eq_exchange", BuiltinExchangeType.FANOUT, true, false, null);
        AMQP.Queue.DeclareOk eq_q = channel.queueDeclare("eq_q", true, false, false, null);
        channel.queueBind("eq_q", "eq_exchange", "");
        CountDownLatch countDownLatch = new CountDownLatch(100);
        for (int j = 0; j < 100; j++) {
            new Thread(() -> {
                for (int i = 0; i < 10000; i++) {
                    try {
                        channel.basicPublish("eq_exchange", "", new AMQP.BasicProperties(), ("msg" + i).getBytes(StandardCharsets.UTF_8));
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                countDownLatch.countDown();
            }).start();
        }
        System.out.println("等待执行完毕");
        countDownLatch.await();
        System.out.println("消息发送成功，等待10秒关闭程序");
        channel.close();
    }
}

